AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")

include("shared.lua")

function ENT:PostEntityPaste( ply, ent, createdents )
self.ply = ply
ply.cores = ply.cores+1
	if(ply.cores>1) then 
		self.Entity:Remove()
	end
end

function ENT:Initialize()
	self.Entity:SetModel("models/props_wasteland/laundry_washer003.mdl") 
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetTrigger( true )
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.Entity:SetUseType(SIMPLE_USE)

	local phy = self.Entity:GetPhysicsObject()
	if phy:IsValid() then 
	phy:Wake() 
	phy:SetMass(10000)
	end
	
	self.Ents = {}
	self.aimPos = Vector(0,0,0)
	self.turretfire = false
	self.broadsidefire = false
	self.seat = nil
	self.driver = nil
	self.lastdriver = nil
	self.core = self.Entity
	self.checktimer = CurTime()
	
	self.hp = 5
	self.maxhp = 5
	self.maxspeed = 100
end

function ENT:SpawnFunction( ply, tr )
	if(ply.cores==0) then
	local ent = ents.Create( "lk_core" )
	local SpawnPos = tr.HitPos + tr.HitNormal * 32
	ent:SetPos( SpawnPos )
	ent:Activate()
	ent.ply = ply
	ent:Spawn()
	
	ply.cores = ply.cores+1
	
	return ent
	else
	return false
	end
end

function WeldedTo( ent1, ent2 )

local constraints = constraint.FindConstraints( ent1, "Weld")

for _, con in pairs( constraints ) do
	if( con.Entity[1].Entity==ent1 and con.Entity[2].Entity==ent2 ) then return true end
	if( con.Entity[2].Entity==ent1 and con.Entity[1].Entity==ent2 ) then return true end
end

return false
end

function ENT:DoDeath( killer )

	if(self.lastdriver and self.lastdriver:IsValid()) then self.lastdriver:AddDeaths(1) end
	if(killer.lastdriver and killer.lastdriver:IsValid()) then killer.lastdriver:AddFrags(1) end
	
	if(killer.lastdriver and killer.lastdriver:IsValid()) then
		killer.lastdriver.salvage = math.min(80,killer.lastdriver.salvage + 20)
		killer.lastdriver.money = killer.lastdriver.money + 700
		MoneyHUDUpdate( killer.lastdriver )
	end
	
	self.driver:ExitVehicle()
	
	for n, ent in pairs( self.Ents ) do
		if( ent:GetClass()=="lk_turret_mount" or ent:GetClass()=="lk_broadside") then
		ent.Repay = false
		ent.ply.money = math.floor(ent.ply.money + ent.price*(self.lastdriver.salvage/100))
		MoneyHUDUpdate( killer.lastdriver )
		SavePlayerData( self.ply )
		end
		if(!ent:IsPlayer()) then
			ent:Remove()
		end
	end
	
	if(self.lastdriver and self.lastdriver:IsValid()) then
		self.lastdriver.salvage = 0
	end
	self.Entity:Remove()
end

function ENT:Think()

	if( self.checktimer < CurTime() ) then
	
		self.Ents = constraint.GetAllConstrainedEntities( self.Entity )
		local hpratio = self.hp/self.maxhp
		self.hp = 5
		self.maxhp = 5
		self.maxspeed = 100
		self.seat = nil
		local multicore_error = false
		
		for n, ent in pairs( self.Ents ) do
			if( ent:IsValid() and ent!=self.Entity) then
				if( ent:GetClass()=="lk_turret_mount" or ent:GetClass()=="lk_broadside") then
					if( ent.core:IsValid() ) then
						if( ent.core==self.Entity) then
							if( !WeldedTo(ent,self.Entity) ) then
								constraint.Weld(ent,self.Entity)
								self.ply:ChatPrint("Welding weapon to core. All weapons must be welded to core!")
							end
						else
						multicore_error = true		--
						ent.core:Remove()			-- Fuck that other core, he was always a dick
						ent.core = self.Entity		--
						end
					else
						ent.core = self.Entity --The turret has no core, give it this one
					end
				elseif( ent:GetClass()=="lk_core" ) then
					multicore_error = true
					ent:Remove()
				elseif( ent:IsVehicle() and self.seat==nil) then
					self.seat = ent
					ent.core = self.Entity
						if(ent:GetDriver():IsPlayer()) then
							ent:GetDriver():ConCommand("airshiphud 1")
						end
				else
					self.maxhp = self.maxhp + GetHealth( ent )
					ent.core = self.Entity
				end
			end
		end
		
		if( multicore_error==true) then
			self.ply:ChatPrint("Cannot have more than one core on a contraption!")
		end
		
		self.hp = self.maxhp*hpratio
	
		self.checktimer = CurTime()+5
	end
	
	self.AimPos = Vector(0,0,0)
	if( self.seat!=nil and self.seat:IsValid() and self.seat:IsVehicle() ) then
		self.driver = self.seat:GetDriver()
		self.lastdriver = self.driver
		if( self.driver:IsValid() ) then
			self.aimPos = self.driver:GetShootPos()+self.seat:GetUp()*self.driver.seatthirdperson*300+self.driver:GetAimVector()*16000
			self.turretfire = self.driver:KeyDown( IN_ATTACK )
			self.broadsidefire = self.driver:KeyDown( IN_ATTACK2 )
			self.driver:ConCommand("airshiphudupdate ".. self.hp .." ".. self.maxhp)
		else
			self.aimPos = Vector(0,0,0)
		end
	end
	
	self.Entity:NextThink( CurTime() + 0.02 )
	return true

end

function ENT:OnRemove()
	local ratio = self.hp/self.maxhp
	for n, ent in pairs( self.Ents ) do
		if(ent:IsValid()) then
			ent.maxhp = GetHealth( ent )
			ent.hp = ent.maxhp*ratio
		end
	end
	self.ply.cores = 0
end